public class Leetcode1824 {

    public static void main(String[] args) {
        System.out.println(minSideJumps1(new int[]{0, 1, 2, 3, 0}));
//        System.out.println(minSideJumps1(new int[]{0, 2, 1, 0, 3, 0}));
    }

    /**
     * 暴力模拟
     *
     * @param obstacles
     * @return
     */
    public int minSideJumps(int[] obstacles) {
        return minSideJumps(obstacles, 2, 1);
    }

    public int minSideJumps(int[] obstacles, int current, int index) {
        if (index >= obstacles.length - 1) {
            return 0;
        }

        if (obstacles[index] == current) {
            if (current == 1) {
                if (obstacles[index - 1] == 2) {
                    return minSideJumps(obstacles, 3, index + 1) + 1;
                }

                if (obstacles[index - 1] == 3) {
                    return minSideJumps(obstacles, 2, index + 1) + 1;
                }

                return Math.min(minSideJumps(obstacles, 3, index + 1), minSideJumps(obstacles, 2, index + 1)) + 1;
            } else if (current == 2) {
                if (obstacles[index - 1] == 1) {
                    return minSideJumps(obstacles, 3, index + 1) + 1;
                }

                if (obstacles[index - 1] == 3) {
                    return minSideJumps(obstacles, 1, index + 1) + 1;
                }

                return Math.min(minSideJumps(obstacles, 3, index + 1), minSideJumps(obstacles, 1, index + 1)) + 1;
            } else if (current == 3) {
                if (obstacles[index - 1] == 2) {
                    return minSideJumps(obstacles, 1, index + 1) + 1;
                }

                if (obstacles[index - 1] == 1) {
                    return minSideJumps(obstacles, 2, index + 1) + 1;
                }

                return Math.min(minSideJumps(obstacles, 1, index + 1), minSideJumps(obstacles, 2, index + 1)) + 1;
            }
        }

        return minSideJumps(obstacles, current, index + 1);
    }

    public static int minSideJumps1(int[] obstacles) {
        int res = 0;

        //当前跑道
        int current = 2;

        loop:
        for (int i = 0; i < obstacles.length; i++) {
            //没有障碍 继续走
            if (obstacles[i] != current) {
                continue;
            }

            res++;
            i--;
            if (current == 1) {
                while (i < obstacles.length) {
                    if (obstacles[i] == 2) {
                        current = 3;
                        continue loop;
                    } else if (obstacles[i] == 3) {
                        current = 2;
                        continue loop;
                    }

                    i++;
                }
            } else if (current == 2) {
                while (i < obstacles.length) {
                    if (obstacles[i] == 3) {
                        current = 1;
                        continue loop;
                    } else if (obstacles[i] == 1) {
                        current = 3;
                        continue loop;
                    }

                    i++;
                }
            } else if (current == 3) {
                while (i < obstacles.length) {
                    if (obstacles[i] == 2) {
                        current = 1;
                        continue loop;
                    } else if (obstacles[i] == 1) {
                        current = 2;
                        continue loop;
                    }

                    i++;
                }
            }

        }

        return res;
    }
}
